package com.dwjm.exam.admin;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/list")
public class AdminListHandler extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        AdminDaoImpl adminDao = new AdminDaoImpl();
        List<Admin> adminList = adminDao.getAllAdmins();
        
        // 构建响应数据
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("code", 200);
        responseMap.put("msg", "获取管理员列表成功");
        responseMap.put("data", adminList);
        
        // 转换为JSON并输出
        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        String json = gson.toJson(responseMap);
        out.write(json);
        out.flush();
        out.close();
    }
} 